package com.bd.rj.frontstage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bd.rj.common.entity.Experience;
import com.bd.rj.frontstage.service.impl.ExperienceServiceImpl;
import com.bd.rj.frontstage.service.impl.PostServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author WBW
 * @date 2020/5/12 15:09
 * 面试经历控制层
 */
@RestController
@RequestMapping("/experience")
public class ExperienceController {

    @Resource
    private ExperienceServiceImpl experienceService;

    @Resource
    private PostServiceImpl postService;


    /**
     *
     * 查询所有面试经历和企业名称
     * @author WBW
     * @return
     */
    @GetMapping("/queryandename")
    public Map<String, Object> QueryAndEname() {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "success");
        map.put("code", 200);
        map.put("data", experienceService.getExperienceAndEname());
        return map;
    }

    /**
     * @author WBW
     * 添加面试经历
     * @param experience 面试经历
     * @return
     */
//    @PostMapping("/add")
//    public Map<String,Object> add(Experience experience){
//        Map<String,Object> map = new HashMap<>();
//        map.put("msg","success");
//        map.put("code",200);
//        map.put("data",experienceService.save(experience));
//        return map;
//    }

    /**
     * @param uid     用户id
     * @param pid     岗位id
     * @param content 面试经历
     * @return
     * @author WBW
     * 添加面试经历
     */
    @PostMapping("/add")
    public Map<String, Object> add(Long uid, Long pid, String content) throws ParseException {
        Map<String, Object> map = new HashMap<>();
        Experience experience = new Experience();
        // 获取 Calendar 实例
        Calendar calendar = Calendar.getInstance();
        // 获取当前时间
        Date now = calendar.getTime();
        // 定义日期时间格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 格式化日期时间并输出
        String formattedDate = dateFormat.format(now);
        Date createTime = dateFormat.parse(formattedDate);
        experience.setUid(uid);
        experience.setPid(pid);
        experience.setContent(content);
        experience.setCreatetime(createTime);
        map.put("msg", "success");
        map.put("code", 200);
        map.put("data", experienceService.save(experience));
        return map;
    }

    /**
     * @author WBW
     * 获取岗位名称
     *
     * @return
     */
    @GetMapping("getpostname")
    public Map<String, Object> getPostName() {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("postname");
        List list = postService.listObjs(wrapper);
        map.put("msg", "success");
        map.put("code", 200);
        map.put("data", list.stream().distinct().collect(Collectors.toList()));
        return map;
    }

    /**
     * @author WBW
     * 获取岗位id
     *
     * @return
     */
    @GetMapping("getpid")
    public Map<String, Object> getPid(String postname) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("id").eq("postname",postname);
        List list = postService.listObjs(wrapper);
        map.put("msg", "success");
        map.put("code", 200);
        map.put("data", list.stream().distinct().reduce((first, second) -> second));
        return map;
    }



}
